package cn.aishua.ui.myKaka;

import org.apache.http.HttpResponse;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import cn.aishua.R;
import cn.aishua.network.HttpRequestUtil;
import cn.aishua.ui.MainActivityGroup;
import cn.aishua.ui.myKaka.task.DataExchangeAsyncTask;
import cn.aishua.util.Constant;
import cn.aishua.util.SharedPrefConst;

public class RegisterNextActivity
    extends Activity
{

    private EditText etUserName;
    private EditText etPassword;
    private EditText etRefferer;

    private String mobileNumber;

    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.my_kaka_register_next );
        initiate();
    }

    private void initiate()
    {
        etUserName = (EditText) findViewById( R.id.my_kaka_register_next_et_user_name );
        etPassword = (EditText) findViewById( R.id.my_kaka_register_next_et_password );
        etRefferer = (EditText) findViewById( R.id.my_kaka_register_next_et_refferer );

        mobileNumber = getIntent().getStringExtra( Constant.EXTRA_STR_MOBILE_NUMBER );
    }

    public void submit( View view )
    {
        String password = etPassword.getText().toString();
        String passwordAgain = password;

        if( invalid( password ) )
        {
            Toast.makeText( this, "请输入6-16位的密码", Toast.LENGTH_SHORT ).show();
        }
        else
        {
            String userName = etUserName.getText().toString();
            String refferer = etRefferer.getText().toString();
            String captcha = getIntent().getStringExtra( Constant.EXTRA_STR_CAPTCHA );

            // task to register 
            new DataExchangeAsyncTask( this, "注册会员" )
            {

                @Override
                protected void handleSuccResponse( String strJson )
                {
                    // task to login
                    new DataExchangeAsyncTask( RegisterNextActivity.this, "会员登陆" )
                    {

                        @Override
                        protected HttpResponse sendRequest( String... params )
                        {
                            return HttpRequestUtil.getInstance().login( params );
                        }

                        @Override
                        protected void handleSuccResponse( String strJson )
                        {
                            SharedPreferences sharedPref = getSharedPreferences( "userInfo", Context.MODE_PRIVATE );
                            Editor editor = sharedPref.edit();
                            editor.putBoolean( SharedPrefConst.SHARED_PREF_BOOL_USER_IS_LOGIN, true );
                            editor.commit();

                            Intent intent = new Intent( RegisterNextActivity.this, MainActivityGroup.class );
                            intent.putExtra( MainActivityGroup.TAB, MainActivityGroup.TAG_USER_INFO );
                            startActivity( intent );
                            finish();
                        }
                    }.execute( etUserName.getText().toString(), etPassword.getText().toString() );

                }

                @Override
                protected HttpResponse sendRequest( String... params )
                {
                    return HttpRequestUtil.getInstance().register( params );
                }
            }.execute( userName, password, passwordAgain, captcha, mobileNumber, refferer );
        }

    }

    private boolean invalid( String password )
    {
        if( password.length() < 6 || password.length() > 16 )
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public void back( View view )
    {
        onBackPressed();
    }

}
